﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("264002")]
    public partial class IpBlackList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblPath.Text = "控制面板 →  <a href=\"IpBlackList.aspx\">IP屏蔽管理</a>";


                BindList(1);
            }
        }

        private void BindList(int PageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var pp = new PagingParam();
            pp.PageSize = this.PageNavigation1.PageSize;
            pp.PageNo = PageNo;

            this.GV_List.DataSource = new IPBlackListManage().BindList(pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        protected void btnAddBL_Click(object sender, EventArgs e)
        {
            var model = new IPBlackList();
            model.Id = 0;
            if (this.RBL_Select.Items[0].Selected)
            {
                if (this.txtIP.Text.Contains("*"))
                {
                    model.IPStart = this.txtIP.Text.Replace("*", "1").Trim();
                    model.IPEnd = this.txtIP.Text.Replace("*", "255").Trim();
                }
                else
                {
                    model.IPStart = this.txtIP.Text.Trim();
                    model.IPEnd = this.txtIP.Text.Trim();
                }
            }
            else
            {
                model.IPStart = this.txtIP0.Text.Trim();
                model.IPEnd = this.txtIP1.Text.Trim();
            }
            model.AdminName = UserManage.Current.UserName;

            var res = new IPBlackListManage().Add(model);

            if (res.Errors.Length == 0)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
            else
            {
                BindList(1);
            }
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = (int) this.GV_List.DataKeys[e.RowIndex].Value;
            var IP0 = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();
            var IP1 = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();

            var model = new IPBlackList();
            model.Id = id;
            model.IPStart = IP0;
            model.IPEnd = IP1;
            model.AdminName = UserManage.Current.UserName;


            var res = new IPBlackListManage().Add(model);

            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
            else
            {
                this.GV_List.EditIndex = -1;
                BindList(this.PageNavigation1.PageNo);
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var ID = this.GV_List.DataKeys[e.RowIndex].Value.ToString();

            new IPBlackListManage().Delete(ID, this.UpdatePanel1);


            MsgBox.Message("删除成功!", this.UpdatePanel1);

            BindList(1);
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GV_List.EditIndex = -1;
            BindList(this.PageNavigation1.PageNo);
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GV_List.EditIndex = e.NewEditIndex;
            BindList(this.PageNavigation1.PageNo);
        }

        protected void PageNavigation1_PageChanging(object src, PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }
    }
}